package com.hbm.usercenter.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.hbm.usercenter.R;
import com.hbm.usercenter.activity.LogAndRegActivity;
import com.hbm.usercenter.base.BaseFragmentCommon;
import com.hbm.usercenter.conf.Constants;
import com.hbm.usercenter.http.MyCallBack;
import com.hbm.usercenter.http.NetClient;
import com.hbm.usercenter.utils.CommonUtils;
import com.hbm.usercenter.utils.LogUtils;
import com.hbm.usercenter.utils.UIUtils;
import com.hbm.usercenter.widget.GoupInputVerify;
import com.hbm.usercenter.widget.GroupInput;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ResetFragment extends BaseFragmentCommon {
    @Bind(R.id.toolbar_title)
    TextView mToolbarTitle;
    @Bind(R.id.phone)
    GoupInputVerify mPhone;
    @Bind(R.id.et_verify)
    GroupInput mEtVerify;
    @Bind(R.id.et_pwd)
    GroupInput mEtPwd;
    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    @Bind(R.id.btn_confirm)
    Button mBtnConfirm;

    private String mpNum;
    private String mPwd;
    private String mCode;

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_reset, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initData() {
        mToolbarTitle.setText("重置密码");
    }

    @Override
    protected void initListener() {
        mPhone.setClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mpNum = mPhone.getPhone();
                checkPhone(mpNum);
                if (checkPhone(mpNum)) {
                    mPhone.setIsCount(true);
                    getCode();
                } else {
                    mPhone.setIsCount(false); ;
                }
            }
        });
    }
    private boolean checkPhone(String mpNum) {
        if (!CommonUtils.isChinaPhoneLegal(mpNum)) {
            Toast.makeText(UIUtils.getContext(), "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
    //获取验证码
    public void getCode() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("action", "reset");
        params.put("phone", mpNum);
        NetClient.getNetClient().callNet(Constants.URLS.SENDCODEURL, new MyCallBack() {
            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(String json) {
                processResult(json, false);
                LogUtils.d(json);

            }
        }, params);
    }

    private void processResult(String json, boolean isLogin) {
        JSONObject rootObject = null;
        try {
            rootObject = new JSONObject(json);
            final String err_msg = rootObject.getString("err_msg");
            String err_code = rootObject.getString("err_code");
            if (err_code.equals("-1")) {
                UIUtils.postTaskSafely(new Runnable() {
                    @Override
                    public void run() {
                        CommonUtils.showShort(UIUtils.getContext(), err_msg);
                    }
                });
                if (isLogin) {
                    startActivity(new Intent(getActivity(), LogAndRegActivity.class));
                } else {

                }
            } else {
                UIUtils.postTaskSafely(new Runnable() {
                    @Override
                    public void run() {
                        CommonUtils.showShort(UIUtils.getContext(), err_msg);
                    }
                });
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {
        mPwd = mEtPwd.getText();
        mCode = mEtVerify.getText();
        sendRequest();
    }

    private void sendRequest() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("phone", mpNum);
        params.put("Pass", mPwd);
        params.put("yanzhengma", mCode);
        NetClient.getNetClient().callNet(Constants.URLS.RESETURL, new MyCallBack() {
            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(String json) {
                processResult(json, true);
                LogUtils.d(json);

            }
        }, params);

    }
}
